# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : AdaBoost+决策树-评估葡萄酒质量.py
# @Author: dongguangwen
# @Date  : 2025-02-08 15:27
import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import accuracy_score


data = pd.read_csv('./data/wine0501.csv')
# print(data.head())
# print(data.info())

data = data[data['Class label'] != 1]
# print(data.info())
x = data[['Alcohol', 'Hue']].copy()
y = data['Class label'].copy()
# print(y)

pre = LabelEncoder()  # 类别转化 (2,3)=>(0,1)
y = pre.fit_transform(y)
# print(y)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=22)

# 模型训练  (决策树和AdaBoost)
tree = DecisionTreeClassifier(criterion='entropy', max_depth=3, random_state=0)
ada = AdaBoostClassifier(estimator=tree, n_estimators=500, learning_rate=0.1, random_state=0)

# 决策树和AdaBoost分类器性能评估
# 决策树性能评估
tree = tree.fit(x_train, y_train)
y_train_pred = tree.predict(x_train)
y_test_pred = tree.predict(x_test)
tree_train = accuracy_score(y_train, y_train_pred)
tree_test = accuracy_score(y_test, y_test_pred)
print('Decision tree train/test accuracies %.3f/%.3f' % (tree_train, tree_test))

# AdaBoost性能评估
ada.fit(x_train, y_train)
y_train_pred = ada.predict(x_train)
y_test_pred = ada.predict(x_test)
ada_train = accuracy_score(y_train, y_train_pred)
ada_test = accuracy_score(y_test, y_test_pred)
print('Adaboost train/test accuracies %.3f/%.3f' % (ada_train, ada_test))

"""
Decision tree train/test accuracies 0.947/0.875
Adaboost train/test accuracies 1.000/0.917
"""
